package hr.fer.ztel.rassus.brbljaonica.gui;

import hr.fer.ztel.rassus.brbljaonica.business.BusinessProvider;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JPasswordField;

import java.awt.CardLayout;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.UIManager;
import java.awt.Color;
import javax.swing.JButton;

import org.jivesoftware.smack.XMPPException;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 * Klasa koja predstavlja panel za login korisnika<br>
 * Korisnik se moze ulogirati sa postojecim accountom ili<br>
 * se registrira kao novi korisnik
 * 
 */
public class LoginPanel extends JPanel {
	public LoginPanel() {
		setLayout(null);
		
		JLabel lblBrbljaonica = new JLabel("Brbljaonica");
		lblBrbljaonica.setFont(new Font("Comic Sans MS", Font.BOLD, 25));
		lblBrbljaonica.setBounds(154, 37, 141, 50);
		add(lblBrbljaonica);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Login", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panel.setBounds(112, 98, 226, 141);
		add(panel);
		panel.setLayout(null);
		
		usernameText = new JTextField();
		usernameText.setBounds(83, 23, 128, 20);
		panel.add(usernameText);
		usernameText.setColumns(10);
		
		passwordText = new JPasswordField();
		passwordText.setBounds(83, 54, 128, 20);
		panel.add(passwordText);
		passwordText.setColumns(10);
		
		serverTextField = new JTextField();
		serverTextField.setBounds(83, 85, 128, 20);
		panel.add(serverTextField);
		serverTextField.setColumns(10);
		
		JLabel lblServer = new JLabel("Server");
		lblServer.setBounds(16, 88, 46, 14);
		panel.add(lblServer);
		
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(16, 26, 57, 14);
		panel.add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(16, 57, 57, 14);
		panel.add(lblPassword);
		
		JButton btnLogIn = new JButton("Log in");
		btnLogIn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					boolean successfulLogin = BusinessProvider.login(
												usernameText.getText(),
												String.valueOf(passwordText.getPassword()),
												serverTextField.getText()
					);
					
					if (successfulLogin) {
						((CardLayout)LoginPanel.this.getParent().getLayout()).show(LoginPanel.this.getParent(), "main");
					}
					else {
						JOptionPane.showMessageDialog(LoginPanel.this, "Invalid username/password", "Error", JOptionPane.ERROR_MESSAGE);
					}
				} catch (XMPPException e1) {
					JOptionPane.showMessageDialog(LoginPanel.this, "Cannot establish connection with server", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnLogIn.setBounds(154, 240, 89, 23);
		add(btnLogIn);
		
		JButton btnRegister = new JButton("Register");
		btnRegister.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				((CardLayout)LoginPanel.this.getParent().getLayout()).show(LoginPanel.this.getParent(), "registration");
			}
		});
		btnRegister.setBounds(249, 240, 89, 23);
		add(btnRegister);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField usernameText;
	private JPasswordField passwordText;
	private JTextField serverTextField;
}
